package com.ruoyi.sale.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.finance.domain.AccountsReceivable;
import com.ruoyi.finance.mapper.AccountsReceivableMapper;
import com.ruoyi.sale.domain.SaleOrder;
import com.ruoyi.sale.domain.SaleOrderProduct;
import com.ruoyi.sale.mapper.SaleOrderMapper;
import com.ruoyi.sale.mapper.SaleOrderProductMapper;
import com.ruoyi.sale.service.ISaleOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * 销售订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-28
 */
@Service
public class SaleOrderServiceImpl implements ISaleOrderService
{
    @Autowired
    private SaleOrderMapper saleOrderMapper;

    @Resource
    private AccountsReceivableMapper accountsReceivableMapper;

    @Resource
    private SaleOrderProductMapper saleOrderProductMapper;

    /**
     * 查询销售订单
     * 
     * @param id 销售订单主键
     * @return 销售订单
     */
    @Override
    public SaleOrder selectSaleOrderById(Long id)
    {
        SaleOrder saleOrder = saleOrderMapper.selectSaleOrderById(id);
        //根据销售订单id获取订单产品集合
        List<SaleOrderProduct> saleOrderProducts = saleOrderProductMapper.selectSaleOrderProductListByOrderId(saleOrder.getId());
        //将产品的集合放入saleOrder对象中
        saleOrder.setGoodsList(saleOrderProducts);

        return saleOrder;
    }

    @Override
    public String updateStatusSaleOrder(SaleOrder saleOrder) {
        saleOrderMapper.updateSaleOrder( saleOrder);
        return null;
    }

    /**
     * 查询销售订单列表
     * 
     * @param saleOrder 销售订单
     * @return 销售订单
     */
    @Override
    public List<SaleOrder> selectSaleOrderList(SaleOrder saleOrder)
    {
        return saleOrderMapper.selectSaleOrderList(saleOrder);
    }

    /**
     * 新增销售订单
     * 
     * @param saleOrder 销售订单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSaleOrder(SaleOrder saleOrder)
    {
        saleOrder.setCreateTime(DateUtils.getNowDate());
        saleOrderMapper.insertSaleOrder(saleOrder);

        int insertId = saleOrderMapper.getInsertId(saleOrder.getOrderNo());
        saleOrder.getGoodsList().forEach(goods -> {
            goods.setOrderId((long) insertId);
            goods.setOrderId(saleOrder.getId());
            saleOrderProductMapper.insertSaleOrderProduct(goods);
        });

        //向应收表添加数据
        AccountsReceivable accountsReceivable = new AccountsReceivable();
        accountsReceivable.setSalesOrderNo(saleOrder.getOrderNo());
        accountsReceivable.setInstitutionName(saleOrder.getCustomerName());
        accountsReceivable.setCustomerId(saleOrder.getCustomerId());
        accountsReceivable.setAmount(saleOrder.getTotalPrice());
        accountsReceivable.setDescription(saleOrder.getRemark());
        accountsReceivable.setCreateTime(saleOrder.getCreateTime());
        accountsReceivableMapper.insertAccountsReceivable(accountsReceivable);


        return 1;
    }

    /**
     * 修改销售订单
     * 
     * @param saleOrder 销售订单
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSaleOrder(SaleOrder saleOrder)
    {
        saleOrder.setUpdateTime(DateUtils.getNowDate());
        saleOrderProductMapper.deleteSaleOrderProductBySaleId(saleOrder.getId());

        //拿到订单号执行插入订单商品
        int insertId = saleOrderMapper.getInsertId(saleOrder.getOrderNo());
        saleOrder.getGoodsList().forEach(goods -> {
            goods.setOrderId((long) insertId);
            goods.setOrderId(saleOrder.getId());
            saleOrderProductMapper.insertSaleOrderProduct(goods);
        });

        return saleOrderMapper.updateSaleOrder(saleOrder);
    }

    /**
     * 批量删除销售订单
     * 
     * @param ids 需要删除的销售订单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSaleOrderByIds(Long[] ids)
    {
        saleOrderProductMapper.deleteSaleOrderProductByIds(ids);
        return saleOrderMapper.deleteSaleOrderByIds(ids);
    }

    /**
     * 删除销售订单信息
     * 
     * @param id 销售订单主键
     * @return 结果
     */
    @Override
    public int deleteSaleOrderById(Long id)
    {
        return saleOrderMapper.deleteSaleOrderById(id);
    }
}
